﻿using Dapper;
using HrEFCore;
using HrEFCore.Models;
using HrIService;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HrService
{
    public class UserService : IUserService
    {
		private readonly MyDbContext myDbContext;
        
        public UserService(MyDbContext myDbConext)
        {
            this.myDbContext = myDbConext;
        }

		public Task<int> AddUser(User user)
		{
			myDbContext.User.Add(user);
			return myDbContext.SaveChangesAsync();
		}

		public Task<int> AddUserRoles(UserRoles userRoles)
		{
			myDbContext.UserRoles.Add(userRoles);
			return myDbContext.SaveChangesAsync();
		}

		public async Task<int> DelUser(short id)//删除用户
		{
			User user = await myDbContext.User.FirstOrDefaultAsync(e=>e.UId == id);
			myDbContext.User.Remove(user);
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<int> DelUserRoles(short id)//删除用户角色
		{
			UserRoles user = await myDbContext.UserRoles.FirstOrDefaultAsync(e=>e.UserID == id);
			myDbContext.UserRoles.Remove(user);
			return await myDbContext.SaveChangesAsync();
		}

		public string InquireName(int UId)
		{
			// 根据UId查询登录人的真实姓名
			var user = myDbContext.User.FirstOrDefault(e => e.UId == UId);
			if (user != null)
			{
				return user.UTrueName.ToString();
			}
			return null;
		}

		public int Login(string UName, string UPassWord)
        {
			//登录
			return myDbContext.User.Where(e => e.UName == UName && e.UPassWord == UPassWord)
	        .Select(e =>e.UId).FirstOrDefault();
		}

		public async Task<View_UserRoles> SelectByUId(short id)//根据ID查找
		{
			return await myDbContext.View_UserRoles.FirstOrDefaultAsync(e => e.UId == id); 
		}

		public async Task<FenYe2<View_UserRoles>> SelectFY(FenYe2<View_UserRoles> fenYe)//分页查询
		{
			using (var conn = myDbContext.Database.GetDbConnection())
			{
				DynamicParameters dp = new DynamicParameters();
				dp.Add("@currentPage", fenYe.CurrentPage);
				dp.Add("@pageSize", fenYe.PageSize);
				dp.Add("@keyName", "UId");
				dp.Add("@tableName", "View_UserRoles");
				dp.Add("@rows", direction: ParameterDirection.Output, dbType: DbType.Int32);
				string sql = "exec [dbo].[proc_FenYe] @currentPage,@pageSize,@keyName,@tableName,@rows out";
				fenYe.List = await conn.QueryAsync<View_UserRoles>(sql, dp);
				fenYe.Rows = dp.Get<int>("rows");
				return fenYe;
			}
		}

		public Task<int> UpdUser(User user)//修改用户
		{
			myDbContext.User.Update(user);
			return myDbContext.SaveChangesAsync();
		}

		public Task<int> UpdUserRoles(UserRoles userRoles)//修改用户角色
		{
			myDbContext.UserRoles.Update(userRoles);
			return myDbContext.SaveChangesAsync();
		}

		public Task<List<User>> UserAllAsync()
        {
            //查询所有
            return myDbContext.User.ToListAsync();
        }

		public Task<List<Roles>> XLRoles()
		{
			return myDbContext.Roles.ToListAsync();
		}
	}
}
